package com.csg.ses.service.ses.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.common.utils.CommonUtil;
import com.csg.ses.common.utils.MybatisPlusUtil;
import com.csg.ses.common.utils.SnowFlakeUtil;
import com.csg.ses.dao.ses.SysRoleRightDao;
import com.csg.ses.entity.ses.SysRoleRightEntity;
import com.csg.ses.service.ses.SysRoleRightService;
import com.csg.ses.vo.common.IdListVo;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import com.csg.ses.vo.list.RoleRightVo;
import com.csg.ses.vo.login.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

/**
 * 角色控制对象
 * @author wangjun
 * @date 2020/11/16
 */
@Service
@RequiredArgsConstructor
public class SysRoleRightServiceImpl extends ServiceImpl<SysRoleRightDao, SysRoleRightEntity> implements SysRoleRightService {
    private final SysRoleRightDao sysRoleRightDao;

    @Override
    public Result retrieve(SysRoleRightEntity right, Long current, Long size) {
        try {
            Page<SysRoleRightEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<SysRoleRightEntity> qw = new QueryWrapper();
            if (!CommonUtil.isEmpty(right.getRoleId())) {
                qw.eq("a.role_id", right.getRoleId());
            }
            if (!CommonUtil.isEmpty(right.getObjectType())) {
                qw.eq("a.object_type", right.getObjectType());
            }
            if (!CommonUtil.isEmpty(right.getPermission())) {
                qw.eq("a.permission", right.getPermission());
            }
            if (!CommonUtil.isEmpty(right.getObjectId())) {
                qw.eq("object_id", right.getObjectId());
            }
            qw.orderByAsc("a.role_id", "a.object_id");
            return new Result(sysRoleRightDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional
    public String del(IdListVo idList) {
        try {
            if (idList != null && idList.getIdList().size() > 0) {
                int num = sysRoleRightDao.deleteBatchIds(idList.getIdList());
                return num + "条记录删除成功！";
            } else {
                return "未指定需要删除的记录！";
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(sysRoleRightDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    @Transactional
    public String save(RoleRightVo roleRightVo) {
        try {
            if (roleRightVo != null && roleRightVo.getRoleRightList().size() > 0) {
                for (SysRoleRightEntity roleRight : roleRightVo.getRoleRightList()) {
                    roleRight.setModifyTime(new Timestamp(System.currentTimeMillis()));
                    roleRight.setModifiedBy(UserContext.getUserView().getLoginCode());
                    if (!CommonUtil.isEmpty(roleRight.getId())) {    //更新
                        sysRoleRightDao.updateById(roleRight);
                    } else {    //新增
                        if (sysRoleRightDao.existsCount(roleRight.getRoleId(), roleRight.getObjectId()) == 0) {
                            roleRight.setId(SnowFlakeUtil.nextStringId());
                            roleRight.setCreateTime(new Timestamp(System.currentTimeMillis()));
                            roleRight.setCreatedBy(UserContext.getUserView().getLoginCode());
                            roleRight.setPermission("Y");
                            roleRight.setObjectType("MENU_ITEM");
                            sysRoleRightDao.insert(roleRight);
                        }
                    }
                }
                return "保存成功！";
            } else {
                return "未传入需要保存的记录！";
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }


    @Override
    @Transactional
    public Result deleteByroleId(IdListVo idList) {
        try {
            if (idList != null && idList.getIdList().size() > 0) {
                int num = sysRoleRightDao.deleteByroleId(idList.getIdList());
                return new Result(num + "条记录删除成功！", ResultStatus.SUCCESS);
            } else {
                return new Result("未指定需要删除的记录！", ResultStatus.FAILED);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}